/*
 * MapMainToolbar.java
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.apps.rtemaster.gui;

import org.fpdev.util.gui.ToolbarButton;
import org.fpdev.apps.rtemaster.gui.map.MapPanel;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
import org.fpdev.apps.rtemaster.RMEvent;
import org.fpdev.apps.rtemaster.RouteMaster;
import org.fpdev.apps.rtemaster.EventTypes;

public class MapMainToolbar extends JToolBar implements ActionListener {

  public static final int ACT_ZOOMRANGE = 1;
  private RouteMaster av_;
  private JTextField locField_;
  // non-radio buttons
  private JButton undoBtn_,  redoBtn_,  drawBtn_,  zInBtn_,  zOutBtn_;
  private ButtonGroup btnGroup_;
  // radio buttons
  private JToggleButton zRangeBtn_;//, selNodeBtn_, selLinkBtn_;
  public MapMainToolbar(RouteMaster av) {
    av_ = av;
    btnGroup_ = new ButtonGroup();
    initComponents();
  }

  public void initComponents() {
    locField_ = new JTextField();
    locField_.addActionListener(this);
    String iconRoot = av_.getEngine().getProperty("5pHome") + "img" + File.separator;

    undoBtn_ = new JButton(new ImageIcon(iconRoot + "undo.png"));
    redoBtn_ = new JButton(new ImageIcon(iconRoot + "redo.png"));
    drawBtn_ = new JButton(new ImageIcon(iconRoot + "redraw.png"));
    zInBtn_ = new JButton(new ImageIcon(iconRoot + "zoomin.png"));
    zOutBtn_ = new JButton(new ImageIcon(iconRoot + "zoomout.png"));

    zRangeBtn_ = new ToolbarButton(new ImageIcon(iconRoot + "zoom.png"), ACT_ZOOMRANGE);
    //selNodeBtn_ = new ToolbarButton("SN", ACT_SELNODE);
    //selLinkBtn_ = new ToolbarButton("SL", ACT_SELLINK);
    zRangeBtn_.setSelected(true);

    locField_.setMaximumSize(new Dimension(200, 24));
    add(locField_);

    addSeparator();

    addButton(undoBtn_, "Undo");
    addButton(redoBtn_, "Redo");
    addButton(drawBtn_, "Refresh Draw Area");
    addButton(zInBtn_, "Fixed Zoom In");
    addButton(zOutBtn_, "Fixed Zoom Out");

    //addSeparator();

    addButton(zRangeBtn_, "Zoom Range");
    //addButton(selNodeBtn_, "Select Node(s)");
    //addButton(selLinkBtn_, "Select Link(s)");

    //this.setBorder(new EmptyBorder(0,0,0,0));
    this.setPreferredSize(new Dimension(0, 40));
  //this.setMaximumSize(new Dimension(1600,36));
  }

  public void addButton(AbstractButton btn, String text) {
    btn.addActionListener(this);
    btn.setMargin(new Insets(0, 0, 0, 0));
    //btn.setBorder(new EtchedBorder());//new EmptyBorder(0,0,0,0));
    //btn.setMinimumSize(new Dimension(24,24));
    //btn.setMaximumSize(new Dimension(30,24));
    btn.setMaximumSize(new Dimension(34, 34));
    btn.setToolTipText(text);
    if (btn instanceof ToolbarButton) {
      btnGroup_.add(btn);
    }
    this.add(btn);
  }

  public void actionPerformed(ActionEvent e) {

    if (e.getSource() == locField_) {
      av_.specifyLocation(locField_.getText());
    }

    // Global Toolbar Actions
    MapPanel map = av_.getGUI().getMapPanel();

    if (e.getSource() == undoBtn_) {
      av_.fireEvent(new RMEvent(EventTypes.EDITMENU_UNDO));
    }
    if (e.getSource() == redoBtn_) {
      av_.fireEvent(new RMEvent(EventTypes.EDITMENU_REDO));
    }
    if (e.getSource() == drawBtn_) {
      map.refresh(true, true, true);
    }
    if (e.getSource() == zInBtn_) {
      map.zoomIn();
      map.repaint();
    }
    if (e.getSource() == zOutBtn_) {
      map.zoomOut();
      map.repaint();
    }

    if (e.getSource() instanceof ToolbarButton) {
      av_.getGUI().setToolbarAction(((ToolbarButton) e.getSource()).getClickAction());
    }
  }

  public void clearSelected() {
    btnGroup_.clearSelection();
  }

  public JButton getUndoButton() { return undoBtn_; }

  public JButton getRedoButton() { return redoBtn_; }

}






















